package com.password.safe.system;

import java.awt.Frame;
import java.io.File;
import java.io.IOException;
import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.password.safe.util.FileUtil;

public class Main {
	private static Logger logger = Logger.getLogger(Main.class);

	/**
	 * Description of this Method
	 * @since  2011-11-20
	 * @author chengpeng.zhang
	 * @param args 
	 */
	public static void main(String[] args) {
		logger.debug("System start at " + new Date());
		
		logger.debug("initializing all beans");
		Resource is = new ClassPathResource("beans.xml");
		BeanFactory factory = new XmlBeanFactory(is);
		logger.debug("all beans are ready...");
		
		logger.debug("ditecting system init or not...");
		File file = FileUtil.getPasswordFile();
		if (!file.exists()) {
			logger.debug("The system have not init, init now....");
			try {
				file.createNewFile();
				logger.debug("create new password's data file success.");
			} catch (IOException e) {
				logger.debug("create password file failed..." + e.getMessage());
			}
			Frame initFrame = (Frame)factory.getBean("initFrame");
			initFrame.setVisible(true);
		} else if (file.exists() && file.length() == 0) {
			logger.debug("system have password file, but it have not init...");
			Frame initFrame = (Frame)factory.getBean("initFrame");
			initFrame.setVisible(true);
		} else {
			logger.debug("The system have already init, run now...");
			Frame loginFrame = (Frame)factory.getBean("loginFrame");
			loginFrame.setVisible(true);
		}
		Frame mainFrame = (Frame)factory.getBean("mainFrame");
		mainFrame.setVisible(false);
		
		Frame updatePasswordFrame = (Frame)factory.getBean("updatePasswordFrame");
		updatePasswordFrame.setVisible(false);
	}

}
